var mg = require('mongoose')
var Schema = mg.Schema
// var moment = require('moment')
// 账号
const accountSchema = Schema({
    account: String,
    phone: String,
    password: String,
    avatar: {type: String, default: 'assets/image/user/index.png'},
    nickname: {type: String, default: 'Avatar'},
    memo: {type: String, default: 'this lazy guy leave nothing'},
    friends: Array
})

// 事件
const eventSchema = Schema({
    icon: String,
    label: String,
    eid: Number,
    hot: Boolean
})

// //好友
// const friendSchema = Schema({
//     icon: String,
//     name: String,
//     fid: Number,
//     label: Array,
//     friends: Array
// })

// 用户活动记录
const activitySchema = Schema({
    timeStart: Date,
    timeEnd: Date,
    locationX: Number,
    locationY: Number,
    location: String,
    eid: Number,
    status: String,
    account: String
}, {toObject: {virtuals: true}}, {toJSON: {virtuals: true}})

// 为用户活动记录建立虚拟属性 base on eventSchema
activitySchema.virtual('event', {
    ref: 'Event',
    localField: 'eid',
    foreignField: 'eid',
    justOne: true
})

// UGC 用户文章
const articalSchema = Schema({
    title: String,
    excerpt: String,
    content: String,
    isPublish: Boolean,
    account: String,
    hot: Boolean,
    createdTime: Date,
    likeNum: {type: Number, default: 0},
    commentNum: {type: Number, default: 0}
}, {toObject: {virtuals: true}}, {toJSON: {virtuals: true}})

const collectionShema = Schema({
    aids: [{type: mg.Schema.ObjectId, ref: 'Artical'}],
    account: String
})

articalSchema.virtual('info', {
    ref: 'Account',
    localField: 'account',
    foreignField: 'account',
    justOne: true
})

// articalSchema.virtual('nickname', {
//     ref: 'Account',
//     localField: 'account',
//     foreignField: 'account',
//     justOne: true
// })

const Model = {
    Account: mg.model('Account', accountSchema),
    Event: mg.model('Event', eventSchema),
    Activity: mg.model('Activity', activitySchema),
    Artical: mg.model('Artical', articalSchema),
    Collection: mg.model('Collection', collectionShema)
}

module.exports = Model
